/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: OAuthController
 * @Package com.rx.core.controller
 * @Description: OAuth2.0
 * @author: 陈锦韬
 * @date: 2021\6\25 0025
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.controller;

import com.rx.core.base.RxBaseConstant;
import com.rx.core.base.RxBaseController;
import com.rx.core.bean.KeyInfo;
import com.rx.core.common.SecretInfo;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.util.RxUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.*;

/**
 * @author: Administrator
 * @Description: OAuth2.0
 * @date: 2021\6\25 0025
 */
@RestController
@RequestMapping(RxBaseConstant.CONFIG_OAUTH_API)
@Slf4j
public class OAuthController extends RxBaseController {
    public static PrivateKey privateKeyForSecret;
    public static PrivateKey privateKey;
    public static PublicKey publicKey;
    public static final String RSA_SECRET = "rsa-secret";

    @Autowired
    com.rx.core.bean.RxTokenConfig rxTokenConfig;

    /**
     * 返回公钥。将秘钥存放
     * @return
     */
    @PostMapping(value = "rsa-public-key")
    public KeyInfo publicKey(){
        // 生成秘钥对
        KeyInfo keyInfo = null;
        try {
            Provider provider = new BouncyCastleProvider();
            KeyPairGenerator keyPairGen  = KeyPairGenerator.getInstance(RxBaseConstant.RSA_ALGORITHM, provider);
            keyPairGen.initialize(1024, new SecureRandom());
            KeyPair keyPair = keyPairGen.generateKeyPair();
            keyInfo =  KeyInfo.build(keyPair.getPublic());

            // TODO: 将私钥存到redis
            privateKey = keyPair.getPrivate();
            RxUtil.nullDo(publicKey,()->publicKey=keyPair.getPublic());
            RxUtil.nullDo(privateKeyForSecret,()->privateKeyForSecret=keyPair.getPrivate());

            Cipher cipher = Cipher.getInstance(RxBaseConstant.RSA_ALGORITHM,provider);
            cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
            byte[] rawPasswordByte = cipher.doFinal("secret".getBytes(RxBaseConstant.CHARSET));
            String en = Base64.getEncoder().encodeToString(rawPasswordByte);
            System.out.println("加密secret:"+en);
            rawPasswordByte = cipher.doFinal("1".getBytes(RxBaseConstant.CHARSET));
            en = Base64.getEncoder().encodeToString(rawPasswordByte);
            System.out.println("加密1:"+en);

        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException e) {
            RxUtil.CheckUtil.throwOut(RESULT_CODE.FAIL);
        }
        return keyInfo;
    }

    /**
     * 返回secret匹配当前秘钥的密文
     * @return
     */
    @PostMapping(value = OAuthController.RSA_SECRET)
    public SecretInfo secret(){

        try {
            Provider provider = new BouncyCastleProvider();
            Cipher cipher = Cipher.getInstance(RxBaseConstant.RSA_ALGORITHM,provider);
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] rawPasswordByte = cipher.doFinal(rxTokenConfig.getOauthSecret().getBytes(RxBaseConstant.CHARSET));
            String en = Base64.getEncoder().encodeToString(rawPasswordByte);
            System.out.println("加密secret:"+en);
            return SecretInfo.build(en);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException e) {
            RxUtil.CheckUtil.throwOut(RESULT_CODE.FAIL);
        }
        return null;
    }
}
